package dk.lbi.videoPlayer
{
// DEF #####################################################
import flash.utils.Timer;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.events.TimerEvent;

import mx.containers.Canvas;
import mx.containers.ApplicationControlBar;
import mx.collections.GroupingCollection;
import mx.controls.AdvancedDataGrid;
import mx.controls.Text;
import mx.controls.Label;
import mx.managers.IBrowserManager;
import mx.managers.BrowserManager;
import mx.events.BrowserChangeEvent;
import mx.events.ListEvent;

import	dk.lbi.localTrace.LocalTrace;
public class VideoPlayer extends Canvas
{
// DEF #####################################################
public	var dg:AdvancedDataGrid=new AdvancedDataGrid();
public	var gc:GroupingCollection=new GroupingCollection();
public	var infoBox:ApplicationControlBar=new ApplicationControlBar();
public	var info:Text=new Text();
[Bindable]
public	var	openB:String="   (";
[Bindable]
public	var	closeB:String=")";
private	var	bm:IBrowserManager;
private var highlightedField:Object;
private var	timer:Timer;
[Bindable	(event="myValueChanged")]
private	function	onTrace(...args):void{LocalTrace.send('gTrace',String(Object(this).constructor),args);}
private	function	goLBi():void{navigateToURL(new URLRequest("http://lbi.com"),"_blank");}
private	function	goMailTo():void{navigateToURL(new URLRequest("mailto:christian.weinreich@framfab.com"),"_blank");}
public function 	VideoPlayer(){onCreationComplete();}
private	function	onCreationComplete():void
{
//	srv.send()
	timer=new Timer(500,1);timer.addEventListener(TimerEvent.TIMER,onCreationCompleteDelayer);timer.start();
	onTrace('	onCreationComplete');
}
private	function	onCreationCompleteDelayer(e:TimerEvent=null):void
{
	onTrace();
	bm=BrowserManager.getInstance();
	bm.init();
	bm.addEventListener(BrowserChangeEvent.BROWSER_URL_CHANGE, onURLChange);
	handleURL(bm.fragment);
	dg.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
	dg.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
	dg.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
	dg.addEventListener(MouseEvent.CLICK,onClick);
	dg.addEventListener(ListEvent.ITEM_ROLL_OVER,onItemRollOver);
	dg.addEventListener(ListEvent.ITEM_ROLL_OUT,onItemRollOut);
	onMouseOut();
}

// Events ##################################################
private function	onItemRollOut(e:ListEvent=null):void
{
	updateHighlightedField();
	onTrace("onItemRollOut	"+highlightedField);
}
private function	onItemRollOver(e:ListEvent=null):void
{
	updateHighlightedField();
	onTrace("onItemRollOver	"+highlightedField);
	if(highlightedField)
	{
		info.htmlText='';
		infoBox.visible=true;
		info.htmlText=
			highlightedField.category+'<br/>'+
			highlightedField.title+' ('+
			highlightedField.length+')<br/>'+
			'<img src="'+highlightedField.img+'"/>'
			'';
		onTrace(info.htmlText);
	}
	else
	{
		infoBox.visible=false;
		info.htmlText='';
	}
}
private function	onMouseMove(e:MouseEvent):void
{
	var offsetX:int=-333;
	var offsetY:int=-175;
	info.x=e.stageX+offsetX;	infoBox.y=e.stageY+offsetY;
	infoBox.x=e.stageX+offsetX;	info.y=e.stageY+offsetY;
}
private function	onMouseOver(e:MouseEvent=null):void
{
	!info.visible?addChild(infoBox):null;
	!info.visible?addChild(info):null;
	info.visible=true;
	onItemRollOver();
	onTrace("onMouseOver")
}
private function	onMouseOut(e:MouseEvent=null):void
{
	info.visible?removeChild(infoBox):null;
	info.visible?removeChild(info):null;
	info.visible=false;
	info.htmlText='';
	onTrace("onMouseOut")
}
private function	onURLChange(e:BrowserChangeEvent=null):void{handleURL(e.target.fragment);}
private	function	handleURL(urlFragmant:String):void
{
	onTrace("	");
	onTrace("	");
	if(!urlFragmant){return;}
	onTrace('	handleURL	'+urlFragmant);
	var path:Array=urlFragmant.toLowerCase().replace(RegExp(/(\s*[\/]\s*)/g), '/').replace(RegExp(/ /g),'').split(RegExp(/[\/]/));
	String(path[0]).length?null:path.shift();
	String(path[path.length-1]).length?null:path.pop();
//	for(;path.length;onTrace(path.shift())){}
	dg.expandAll();dg.collapseAll();	//	it is a stupid thing, but without this the deeplinl part is not working.
	onTrace('	handleURL	'+urlFragmant);
	String(path[0]).length?dg.expandItem(dg.getParentItem(find('category',path[0])),true):null;
	dg.validateNow();
	path.shift();
	if(!path.length)return;
	String(path[0]).length?dg.selectedItem=find('subcat',path[0]):null;
	dg.validateNow();
	path.shift();
	dg.scrollToIndex(dg.selectedIndex);
}
private	function	updateHighlightedField():void
{
//	onTrace('	looking for a highlighted field');
	if(!gc.source){return;}
	for(var i:uint=gc.source.length;i--;)
	{
		if(dg.isItemHighlighted(gc.source[i])){highlightedField=Object(gc.source[i]);return;}
	}
	highlightedField=null;
}
private function	find(attributumName:String,attributumValue:String):Object
{
	if(!attributumName)return null;
	for(var i:uint=gc.source.length;i--;)
	{
//		onTrace('\t'+gc.source[i][attributumName].toLowerCase().replace(RegExp(/(\s*[\/]\s*)/g), '/').replace(RegExp(/ /g),'')+' | '+attributumValue);
		if(gc.source[i][attributumName].toLowerCase().replace(RegExp(/(\s*[\/]\s*)/g), '/').replace(RegExp(/ /g),'')==attributumValue){return gc.source[i];}
	}
	throw new Error('NOT ON THE LIST\n');
}
/////////////////////////////////////////
public	function	onClick(e:MouseEvent=null):void
{
//	highlightedField?bm.setFragment(String('/'+highlightedField['category']+'/'+highlightedField['subcat']+'/').replace(RegExp(/ /g),'')):null;
	highlightedField?bm.setFragment(String('/'+highlightedField['category']+'/'+highlightedField['subcat']+'/')):null;
	if(highlightedField!=null || dg.selectedIndex<0){return;}
	var x:int=dg.rowCount;
	if(x==dg.rowCount)
	{
		dg.expandItem(dg.selectedItem,!dg.isItemOpen(dg.selectedItem));
	}
}
// END #####################################################
}	//class
}	//package
// END #####################################################